Building a Tip of the Day Application (Part 3): Submitting Tips to the Web Service
Table of Contents
Searching for Tips
Building the searchform and searchresult systems
are perhaps the most complex portions of the application. Sending the
search to the server is a simple process: You'll simply bind the keyword_txt TextInput
field to a WebServiceConnector component and use a behavior to trigger
the web service when a user clicks the Search button. Then you'll process
the returned XML results.
Calling the Search
Now you will add the code to call the search from the SWF file. Bind the search keyword text entry field to its corresponding parameter in the web service as follows:
- Select the presentation screen and open the Web Services
panel (Control+Shift+F10). Expand the
tipService, right-clickgetTipsByKeyword, add a method call, and close the Web Services panel. - Move the new WebServiceConnector component off the Stage with the
other data components and assign it an instance name of
getTipsByKeyword_wsc. - Open the Bindings tab in the Component Inspector panel and click the Add Binding button to open the Add Binding dialog box.
- Select the
Keyword : stringfrom the params object and click OK. -
Bind
params.Keywordto thekeyword_txtTextInput instance on the searchform screen. In the Bindings tab, select params.Keyword, and then double-click the field to the right of bound to. The Bound To dialog box opens. Find thekeyword_txtinstance in the tree and select it. (See Figure 3.) Click OK to close the Bound To dialog box.
Figure 3. Binding params.keyword to keyword_txt.
Add the Trigger Data Source behavior to the searchform screen as follows:
- On the searchform screen, select the Search button. Open
the Behaviors panel, click the Add Behavior button and select Data > Trigger
Data Source from the contextual menu. In the Select Data Source component
pane of the Trigger Data Source dialog box, select the
getTipsByKeyword_wscinstance and click OK. -
Make sure the Trigger Data Source behavior is above the Go to Slide behavior for the Search button on the searchform screen.
Because this Search button already has a behavior placed on it, confirm that the Trigger Data Source behavior appears before the Go to Slide behavior in the list in the Behaviors panel. To reorder behaviors, you can select the behavior you want in the Behaviors panel and click the Move Up or Move Down button in the upper right corner of the panel.
Figure 4. Behaviors for the Search button on the searchform screen
Receiving XML Results from a Web Service
If you are receiving XML from a web service, it is easy to consume the data by converting the packet into a Flash XML object. Just save the results as an XML packet in the DataHolder component, and then bind data to the XML packet.
Also, before you can bind the data from the XML packet, you have to import an XML schema so Flash can build the structure of the XML document. In this section, you will save and import a sample schema from XML generated by a trace statement that you pull from the Output panel.
- Select the presentation screen and click the DataHolder instance. Open the Component Inspector panel and click the Schema tab.
- Click the Add a Component Property (+) button in the upper left corner and, in the lower pane, set the field name for the new field to keywordresults. Set the field’s data type to XML.
-
Select the
getTipsByKeyword_wscinstance, and open the Actions panel. Enter the following ActionScript to trace results.on (result) { trace(this.results); } - Test your movie. When you do a keyword search, Macromedia Flash displays the XML packet sent back from the server to the Output window. Make sure you have multiple results and not a single result. (You can try searching for a blank string to return the maximum number of records.)
- Copy the XML packet from the Output window and save it to an XML file on your hard drive.
- Import the schema from the XML file you just saved as follows:
- Select the DataHolder instance on the presentation screen.
- With the
keywordresultsproperty selected in the upper Schema pane of the Component Inspector panel, click the Import a Schema from a Sample XML File button (located near the upper right corner of the Component Inspector panel, just below the Schema tab). This launches a file browser where you can locate the XML file you just saved on your hard drive. - Choose the XML file you saved and click Open. Now you can expand
the schema for the
keywordresultsproperty and see its structure.
- Remove the white space from the XML and set the value of
keywordresultsto the XML packet using ActionScript as follows:- Click the
getTipsByKeyword_wscinstance on the Stage. -
Open the Actions panel and replace the existing code with the code below:
on (result) { XML.prototype.ignoreWhite = true; this._parent.dataHolder_dh.keywordresults = new XML(this.results); }
- Click the
-
The first line of code tells Macromedia Flash to ignore white space in XML documents. (When working with XML, Flash can be sensitive to excessive white space; this command tells Flash to ignore all nodes that contain only white space and no data.) The second line of code sets the value of the keywordresults property in the DataHolder instance to the results of the XML packet. Because you’re using the XML() constructor, Flash converts the packet into a Flash XML object, which you can bind and manipulate in Flash.
-
Select the
dataHolder_dhinstance on the presentation screen and open the Bindings tab of the Component Inspector panel. Add a new binding forkeywordresults > results > tip. Set the direction of the binding to out. -
Double-click the bound to field in the lower pane of the Bindings tab to open the Bound To dialog box. In the Component Path pane, select the
results_lsList component and then selectdataProvideras the Schema location.By setting a List component’s
dataProviderproperty, you are able to set all of the list’s items at once. This means that once you've bound the List component to the tip array, the List contains each tip passed back from the web service.If you test the SWF file now, search, and receive multiple results, you will see that all of the fields from the XML document are merged into the label property for each record. To rearrange the fields before they are bound to the List component then, use a formatter. You can find a formatter in the Bindings tab of the Component Inspector panel.
Figure 6. Formatter for the keywordresults.results.tip data object.
-
Set the formatter to Rearrange Fields as shown in Figure 6. This rearranges the fields from the XML object before it is bound to the List component. For this example, set the Fields definition value to the following:
data=TipDateTime;label=TipTitle
You are binding the data property of the List component to the
TipDateTimefield from the web service’s returned XML. Likewise, you are setting the label for the List component to theTipTitlefrom the DataHolder component.
Figure 5. New dataHolder_dh binding to keywordresults.results.tip